草庐IT

C++ 对和指针

全部标签

c# - 如何在C#中使用C++ DLL导出的接口(interface)指针

我有一个用C++编写的DLL,它导出一个函数CreateRisk。该函数返回一个接口(interface)指针,如下所示:extern"C"{__declspec(dllexport)IRisk*__stdcallCreateRisk(){returnnewRisk();}}IRisk派生自IUnknown并具有自定义方法Calculate:classIRisk:publicIUnknown{public:virtualint__stdcallCalculate(inti,doubles)=0;};类Risk实现IRisk接口(interface)(这里省略实现)。我想要的是在C#中调

c++ - 将共享指针与在另一个函数中分配的内存一起使用

我有一些遗留时代的代码在工作,它接收一个双指针并为其分配内存。它的一个简短示例如下所示:structLegacyObj{inta;doubleb;};voidLegacyAllocator(LegacyObj**ppObj){*ppObj=(LegacyObj*)malloc(sizeof(LegacyObj));}voidLegacyDeleter(LegacyObj**ppObj){free(*ppObj);}实际的LegacyAllocator函数大约有100行,混合了从文件中读取和创建LegacyObj指针的链表的功能,我现在无法通过重写来解决这个问题。然而,我想使这个函数的使

c++ - "could not convert template argument"指针参数错误,即使使用强制转换

假设我有一个声明如下的模板类:templatestructy{int*b;y(){b=x;}}我确实需要模板参数是一个常量内存地址——它是一个嵌入式代码。如果我尝试像这样实例化它:(编译器是带有-std=gnu++11的gcc4.8.1)yc;我会收到错误消息“无法将模板参数‘1’转换为‘int*’”,这没关系,而且符合标准。我明白。我的问题是转换为指针也不起作用:yd;y(1)>e;error:couldnotconverttemplateargument'1u'to'int*'在这两种情况下。这是为什么?模板参数已经转换,不是吗? 最佳答案

c++ - 来自成员函数指针的 QMetaMethod

给定一个指向QObject派生类的方法的指针:有没有办法获取指针指向的方法的QMetaMethod?我基本上是在寻找类似QMetaMethod::fromSignal的函数,但用于插槽。注意:我尝试使用QMetaObject::IndexOfMethod通过static_metacall获取索引并将其用于QMetaObject::method:void(Class::*method)()=&Class::method;intmethodIndex=-1;void*metaArgs[]={&methodIndex,reinterpret_cast(&method)};constQMeta

c++ - 未指定的指针转换在 C++14 中的行为如何?

某些指针转换的结果被描述为未指定。例如,[expr.static.cast]/13:Aprvalueoftype“pointertocv1void”canbeconvertedtoaprvalueoftype“pointertocv2T,”[...]IftheoriginalpointervaluerepresentstheaddressAofabyteinmemoryandAsatisfiesthealignmentrequirementofT,thentheresultingpointervaluerepresentsthesameaddressastheoriginalpoint

c++ 指向函数的指针没有改变

我已经定义了一些函数并且我打印它们的地址是这样的:#include#includeusingstd::cout;std::stringfunc(){return"helloworld\n";}intfunc2(intn){if(n==0){cout当我打印函数的名称(地址)时,它们都会在我的编译器(Mingwgcc4.8)上打印1。可以吗还是应该有所不同? 最佳答案 不存在operator的重载对于std::ostream它需要一个函数指针。因此operator过载是首选。函数的地址总是计算为true转换为bool时.因此,打印了1

c++ - 将指向结构的指针转换为数组指针是否有效

给定一个聚合结构/类,其中每个成员变量都是相同的数据类型:structMatrixStack{Matrix4x4translation{...};Matrix4x4rotation{...};Matrix4x4projection{...};}matrixStack;将其转换为成员数组的有效性如何?例如constMatrix4x4*ptr=reinterpret_cast(&matrixStack);assert(ptr==&matrixStack.translation);assert(ptr+1==&matrixStack.rotation);assert(ptr+2==&matr

c++ - 指向 deque<int>::push_back 的指针

#include#includeusingnamespacestd;main(){typedefvoid(deque::*func_ptr)(int);func_ptrfptr=&deque::push_back;}我试图获取指向该函数的指针,但出现编译错误error:cannotconvert‘void(std::deque::*)(constvalue_type&){akavoid(std::deque::*)(constint&)}’to‘func_ptr{akavoid(std::deque::*)(int)}’ininitializationfunc_ptrfptr=&deq

c++ - 向上转换指向数据成员的指针及其多态行为

我试图将指向派生类数据成员的指针强制转换为指向基类数据成员的指针,但以下代码无法编译:classBase{public:virtualvoidf(){}};classDerived:publicBase{public:voidf()override{}};classEnclosing{public:Derivedmember;};intmain(){DerivedEnclosing::*p=&Enclosing::member;autobp=static_cast(p);//compileerror}所以我改用reinterpret_cast,代码编译:autobp=reinterpr

c++ - 如何在编译时保存指向成员的指针?

考虑以下代码templatestructA{typedefTvalue_type;//OK.saveTtovalue_typestaticconstintsize=N;//OK.saveNtosize};看,如果此参数是类型名称或整数值,则可以保存任何模板参数。问题是指向成员的指针是一个偏移量,即整数。现在我想在编译时保存任何指向成员的指针:structFoo{intm;intr;};templatestructB{//NextstatementDOESNOTWORK!staticintFoo::*constsaved_ptr_to_member=ptr_to_member;};//E